package com.neusoft.a132wxyorderingdish.activity;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.a132wxyorderingdish.R;
import com.neusoft.a132wxyorderingdish.adapter.FoodListAdapter;
import com.neusoft.a132wxyorderingdish.beans.FoodBean;
import com.neusoft.a132wxyorderingdish.listener.RetrofitListener;
import com.neusoft.a132wxyorderingdish.model.ShopModel;

import java.util.List;

public class SearchResultActivity extends BaseActivity implements RetrofitListener<List<FoodBean>> {

    private RecyclerView recyclerView;
    private ImageButton imageButton;
    private TextView textView;
    private String search;
    private FoodListAdapter foodListAdapter;
    @Override
    void initViews() {
        setLayout_file(R.layout.activity_search_result);
        imageButton=findViewById(R.id.search_back);
        textView=findViewById(R.id.search_this);
        recyclerView=findViewById(R.id.search_rv);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    void initEvents() {
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SearchResultActivity.this.finish();
            }
        });
    }

    @Override
    void initData() {
        search=getIntent().getStringExtra("search");
        ShopModel shopModel=new ShopModel();
        shopModel.getFoodBySearch(search,this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onSuccess(List<FoodBean> foodBeans, int flag) {
        if(foodBeans!=null&&foodBeans.size()>0){
            foodListAdapter=new FoodListAdapter(SearchResultActivity.this,foodBeans);
            recyclerView.setAdapter(foodListAdapter);
        }
        else {
            Toast.makeText(this, "没有搜索结果", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    public void onFail() {

    }

}
